package com.ruoyi.service.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.service.utils.ServiceUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.domain.Execute;
import com.ruoyi.service.service.IExecuteService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 执行目录电价（市场电量以外的电量） 信息操作处理
 * 
 * @author john
 * @date 2019-01-04
 */
@Controller
@RequestMapping("/service/execute")
public class ExecuteController extends BaseController
{
    private String prefix = "service/execute";
	
	@Autowired
	private IExecuteService executeService;
	@Autowired
	private ServiceUtils utils;
	
	@GetMapping()
	public String execute()
	{
	    return prefix + "/execute";
	}
	
	/**
	 * 查询执行目录电价（市场电量以外的电量）列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Execute execute)
	{
		startPage();
        List<Execute> list = executeService.selectExecuteList(execute);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出执行目录电价（市场电量以外的电量）列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Execute execute)
    {
    	List<Execute> list = executeService.selectExecuteList(execute);
        ExcelUtil<Execute> util = new ExcelUtil<Execute>(Execute.class);
        return util.exportExcel(list, "execute");
    }
	
	/**
	 * 新增执行目录电价（市场电量以外的电量）
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存执行目录电价（市场电量以外的电量）
	 */
	@Log(title = "执行目录电价（市场电量以外的电量）", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Execute execute)
	{		
		return toAjax(executeService.insertExecute(execute));
	}

	/**
	 * 修改执行目录电价（市场电量以外的电量）
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Execute execute = executeService.selectExecuteById(id);
		mmap.put("month", utils.getMonth(execute.getBasicsId()));
		mmap.put("execute", execute);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存执行目录电价（市场电量以外的电量）
	 */
	@Log(title = "执行目录电价（市场电量以外的电量）", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Execute execute)
	{

		execute.setFTotal(execute.getFF().add(execute.getFP()).add(execute.getFG()));
		execute.setPTotal(execute.getPF().add(execute.getPP()).add(execute.getPG()));
		execute.setKTotal(execute.getKF().add(execute.getKP()).add(execute.getKG()));
		execute.setEleTotal(execute.getFTotal().add(execute.getPTotal()).add(execute.getKTotal()));
		execute.setUpAccount (execute.getFactoryPrice().multiply(execute.getEleTotal()));
		execute.setSPAccount(execute.getSPPrice().multiply(execute.getEleTotal()));
		execute.setJJAccount(execute.getJJAverage().multiply(execute.getEleTotal()));
//		execute.setEleAccount(execute.getDHAverage().multiply(execute.getEleTotal()));

		//        用户承担电费金额eleAcount = 上网电费upAccount + 输配收入金额sPAccount
		execute.setEleAccount(execute.getUpAccount().add(execute.getSPAccount()));
//        到户均价dHAverage = 用户承担电费金额eleAcount / 留存电量合计keepTotal
		BigDecimal TradeTotal = execute.getEleTotal();
		if (TradeTotal == BigDecimal.valueOf(0)) {
			execute.setDHAverage(BigDecimal.ZERO);
		}else{
			execute.setDHAverage(execute.getEleAccount().divide(TradeTotal).setScale(6,BigDecimal.ROUND_HALF_UP));
		}
		int i = executeService.updateExecute(execute);
		utils.total(execute.getPointId(),execute.getBasicsId());
		return toAjax(i);
	}
	
	/**
	 * 删除执行目录电价（市场电量以外的电量）
	 */
	@Log(title = "执行目录电价（市场电量以外的电量）", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(executeService.deleteExecuteByIds(ids));
	}
	
}
